package com.ms.game.slot.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xpec.c4.game.utility.GameUtility;

/**
 *  搓搓樂
 * @author TW15012
 *
 */
public class SlotTwistScreenSymbolMaker 
{
	public synchronized ArrayList<Integer> getScreenSymbol(int row , int col , int bingosize , int bingosymbolid , ArrayList<Integer> others)
	{
		ArrayList<Integer> screen = new ArrayList<Integer>();
		int totalsize = row * col;
		int needsymbol = totalsize / bingosize;
		int othersymbol =needsymbol - 1;
		
		
		//做一個temp出來
		ArrayList<Integer> temp = new ArrayList<Integer>();
		for(int value:others)
		{
			//排除中獎id
			if(value==bingosymbolid)
			{
				continue;
			}
			temp.add(value);
		}
		
		//先取出不中獎的四個值
		Collections.shuffle(temp);
		List<Integer> mix =temp.subList(0, othersymbol);
		
		//塞入中獎
		for(int i=0;i<bingosize;i++)
		{
			screen.add(bingosymbolid);
		}
		
		//塞入魚目混珠
		int size =mix.size() ;
		for(int j=0;j<size;j++)
		{
			int value=mix.get(j);
			for(int k=0;k<bingosize-1;k++)
			{
				screen.add(value);
			}
		}
		
		//打散兩次
		Collections.shuffle(screen);
		Collections.shuffle(screen);
		
		
		//補齊
		for(int mixid:mix)
		{
			screen.add(mixid);
		}
		
		
		return screen;
	}
	
	/**
	 * jp單獨計算
	 * @param row
	 * @param col
	 * @param bingosize
	 * @param bingosymbolid
	 * @param others
	 * @param jp
	 * @return
	 */
	public synchronized ArrayList<Integer> getScreenSymbolByJPrand(int row , int col , int bingosize , int bingosymbolid , int jprate , ArrayList<Integer> others , ArrayList<Integer> jpsymbol)
	{
		
		int totalsize = row * col;
		int needsymbol = totalsize / bingosize;
		int othersymbol =needsymbol - 2;
		
		ArrayList<Integer> screen = new ArrayList<Integer>();
		ArrayList<Integer> jpsb = new ArrayList<Integer>();
		
		
		//做一個temp出來
		ArrayList<Integer> temp = new ArrayList<Integer>();
		for(int value:others)
		{
			//排除中獎id
			if(value==bingosymbolid)
			{
				continue;
			}
			temp.add(value);
		}
		
		
		//中jp直接跑，不考慮jp狀況 
		if(
				!isSpecSymbol(jpsymbol , bingosymbolid)
		)
		{
				int start = GameUtility.randomInt(0, 100-jprate);
				int end = start + jprate;
				for(int sid:jpsymbol)
				{
					int jpshow = GameUtility.randomInt(0, 100);
					if(jpshow>=start && jpshow<end)
					{
						jpsb.add(sid);
					}
				}
				
				if(jpsb.size()==0)
				{
					othersymbol++;
				}
				
		}
		else
		{
			othersymbol++;
		}
		
		
		//先取出不中獎的個數
		Collections.shuffle(temp);
		List<Integer> mix =temp.subList(0, othersymbol);
		
		if(jpsb.size()>0)
		{
			mix.addAll(jpsb);
		}
		
		//塞入中獎
		for(int i=0;i<bingosize;i++)
		{
			screen.add(bingosymbolid);
		}
		
		
		//塞入魚目混珠
		int size =mix.size() ;
		for(int j=0;j<size;j++)
		{
			int value=mix.get(j);
			for(int k=0;k<bingosize-1;k++)
			{
				screen.add(value);
			}
		}
				
		//打散兩次
		Collections.shuffle(screen);
		Collections.shuffle(screen);
				
				
		//補齊
		for(int mixid:mix)
		{
			screen.add(mixid);
		}
		
		return screen;
	}
	
	
	private boolean isSpecSymbol(ArrayList<Integer> jpsymbol , int bingoid)
	{
		for(int jpid:jpsymbol)
		{
			if(bingoid == jpid)
			{
				return true;
			}
		}
		
		return false;
	}
}
